/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package bea.cliente.core;

import bea.cliente.character.Player;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ivan da Silveira
 */
public class Game extends Core {
    // player variables
    private Player player;
    private ChaseCamera chaseCam;
    // STATES
    // physics
    private BulletAppState bulletAppState;
    private PhysicsSpace physicsSpace;
    // worldManager
    private WorldManagerState worldManagerState;
    // guiManager
    private GuiManagerState guiManagerState;
    // networkManager
    private NetworkManagerState networkManagerState;

    public Game() {
        super();
    }

    @Override
    public void gameInit() {
        // SET LOGGER OFF
        java.util.logging.Logger.getLogger("").setLevel(Level.SEVERE);
        
        //inputManager.setCursorVisible(true);
        getCamera().setFrustumFar(300);
        flyCam.setMoveSpeed(100);
        cam.setLocation(new Vector3f(50, 50, 50));
        cam.setLocation(new Vector3f(50, 50, 50));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
        initGameStates();
    }
    
    private void initGameStates(){
        // physics state
        bulletAppState = new BulletAppState();
        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
        physicsSpace = bulletAppState.getPhysicsSpace();
        stateManager.attach(bulletAppState);
        
        // world manager state
        worldManagerState = new WorldManagerState(this);
        stateManager.attach(worldManagerState);
        
        // gui manager state
        guiManagerState = new GuiManagerState(this);
        //stateManager.attach(guiManagerState);
        
        networkManagerState = new NetworkManagerState(this);
        //stateManager.attach(networkManagerState);
        
        Memory.put(Memory.NETWORK_MANAGER, networkManagerState);
        Memory.put(Memory.GUI_MANAGER, guiManagerState);
        Memory.put(Memory.WORLD_MANAGER, worldManagerState);
    }
    
    private void initCharacter() {
        // disabilita a camera livre
        flyCam.setEnabled(false);
        flyCam.setMoveSpeed(100);
        player = new Player();
        player.initPlayer(this);
        chaseCam = new ChaseCamera(cam, player, inputManager);
        rootNode.attachChild(player);
        player.getCharacterControl().setPhysicsLocation(new Vector3f(50, 5, 50));
    }

    /**
     * 
     * @return O Espaço fisico do jogo
     */
    public PhysicsSpace getPhysicsSpace() {
        return bulletAppState.getPhysicsSpace();
    }

    /**
     * 
     * @return O personagem atual do jogador
     */
    public Player getPlayer() {
        return player;
    }

    @Override
    public void gameUpdate(float tpf) {
    }

    @Override
    public void gameRender(RenderManager rm) {
    }

    /**
     * static method
     * @param args Argumentos de inicialização do jogo
     */
    public static void main(String args[]) {
        Game game = new Game();
        // guarda o objeto game para futuras consultas
        Memory.put(Memory.GAME, game);
        try {
            game.start();
        } catch (Exception e) {
            Logger.getLogger(Core.class.getName()).log(Level.SEVERE, "General error on Game Start!", e.getMessage());
            System.exit(1);
        }
    }
}
